﻿using DocumentFormat.OpenXml.Office2010.Excel;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.RecurringJobs;
using NP.BPMReportPlatform.TaxManage.Dto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.ObjectMapping;

namespace NP.BPMReportPlatform.TaxManage
{
    public class TaxApplyContentService : BPMReportPlatformAppService, ITaxApplyContentService
    {
        private readonly SqlSugarClient _dbBusiness;

        public TaxApplyContentService(ISqlSugarAppService sqlSugarAppService)
        {
            _dbBusiness = sqlSugarAppService.GetBusinessInstance();

        }

        public async Task DeleteAsync(Guid id)
        {
            await _dbBusiness.Deleteable<TaxApplyContent>(id).ExecuteCommandAsync();
        }

        public async Task EditAsync(TaxApplyContent input)
        {
            await _dbBusiness.Updateable<TaxApplyContent>(input)
                .UpdateColumns(t => new
                {
                    t.TaxClassificationCode,
                    t.IsCanEdit,
                    t.TaxRate,
                    t.IsAssociatedAccount,
                    t.ApplyContent,
                    t.TicketContent,
                    t.SAPCode,
                    t.Txt1,
                    t.Txt2,

                })
                .ExecuteCommandAsync();
        }

        public async Task<PagedResultDto<TaxApplyContentDto>> GetAllAsync(TaxApplyContentSearchDto input)
        {
            RefAsync<int> totalCount = 0;
            var result = await _dbBusiness.Queryable<TaxApplyContent>()
                 .WhereIF(!string.IsNullOrEmpty(input.TicketContent), t => t.TicketContent.Contains(input.TicketContent))
                 .WhereIF(!string.IsNullOrEmpty(input.ApplyContent), t => t.ApplyContent.Contains(input.ApplyContent))
                 .WhereIF(input.IsAssociatedAccount.HasValue, t => t.IsAssociatedAccount == input.IsAssociatedAccount)
                 .WhereIF(!string.IsNullOrEmpty(input.TaxClassificationCode), t => t.TaxClassificationCode.Contains(input.TaxClassificationCode))
                .OrderBy(t => t.CreateTime, OrderByType.Desc) 
                 .ToPageListAsync(input.PageIndex, input.PageSize, totalCount);
            var mapperList = ObjectMapper.Map<List<TaxApplyContent>, List<TaxApplyContentDto>>(result);

            return new PagedResultDto<TaxApplyContentDto>()
            {
                TotalCount = totalCount,
                Items = mapperList
            };

        }

        public async Task SaveAsync(TaxApplyContent input)
        {
            input.Id = Guid.NewGuid();
            input.CreateTime = DateTime.Now;
            await _dbBusiness.Insertable<TaxApplyContent>(input).ExecuteCommandAsync();
        }
    }
}
